package com.iw.adoms.action;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import com.iw.adoms.entity.Department;
import com.iw.core.security.entity.Users;

public class BaseAction {

	@InitBinder
	public void InitBinder(WebDataBinder dataBinder) {
		try {
			dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	protected Users getCurrentUser(HttpServletRequest request) {
		Users user = (Users) request.getSession().getAttribute("USER");
		return user;
	}

	protected Department getCurrentDepartment(HttpServletRequest request) {
		Department department = this.getCurrentUser(request).getDepartment();
		return department;
	}

	protected Integer getCurrentDepartmentId(HttpServletRequest request) {
		Integer departmentId = this.getCurrentDepartment(request).getId();
		return departmentId;
	}

}
